//
//  ChooseView.m
//  XDBaseFrameWork
//
//  Created by xindao on 14-9-22.
//  Copyright (c) 2014年 XD-XY. All rights reserved.
//

#import "ChooseView.h"
@implementation ChooseView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        dataArray = [[NSMutableArray alloc] init];

        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_MAINSCREEN_HEIGHT-UI_NAVIGATION_BAR_HEIGHT)];
        _bgView.backgroundColor = [UIColor clearColor];
        [self addSubview:_bgView];

        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideSelf)];
        [_bgView addGestureRecognizer:tap];

        littleBgView = [UIView initViewRect:CGRectMake(0, 0, UI_SCREEN_WIDTH, 0) andBackGroudColor:[UIColor blackColor]];
        littleBgView.alpha = .9f;
        [_bgView addSubview:littleBgView];

        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_MAINSCREEN_HEIGHT-UI_NAVIGATION_BAR_HEIGHT) style:UITableViewStylePlain];
        _myTableView.dataSource = self;
        _myTableView.delegate = self;
        _myTableView.scrollEnabled = NO;
//        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableView.backgroundColor = [UIColor clearColor];
        [self addSubview:_myTableView];


    }
    return self;
}

- (void)hideSelf{
    [_delegate chooseType:@"hide" data:nil];
}

- (void)showWithType:(NSString *)type array:(NSArray *)array title:(NSString *)title{
    self.hidden = NO;
    _type = type;
    titleStr = title;
    if ([type isEqualToString:@"taocan"]) {
        dataArray = [NSMutableArray arrayWithArray:array];
    }else if ([type isEqualToString:@"dizhi"]) {
        dataArray = [NSMutableArray arrayWithArray:array];
    }else if ([type isEqualToString:@"otherType"]) {
        dataArray = [NSMutableArray arrayWithArray:@[@{@"name":@"离我最近",@"type":@"Range"},
                                                     @{@"name":@"接单量",@"type":@"OrderCount"},
                                                     @{@"name":@"好评率",@"type":@"ReviewAvg"},
                                                     @{@"name":@"价格最高",@"type":@"PriceTop"},
                                                     @{@"name":@"价格最低",@"type":@"PriceLow"}]];
    }
    [_myTableView reloadData];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    _myTableView.frame = CGRectMake(0, 0, UI_SCREEN_WIDTH, 40*dataArray.count);
    littleBgView.frame = _myTableView.frame;
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * cellIde = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIde];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIde];
        cell.textLabel.frame = CGRectMake(20, 10, 200, 20);
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.backgroundColor = [UIColor clearColor];
    }
    if ([_type isEqualToString:@"taocan"]) {
        cell.textLabel.text = dataArray[indexPath.row][@"SceneName"];
    }else if ([_type isEqualToString:@"dizhi"]) {
        cell.textLabel.text = dataArray[indexPath.row][@"CityName"];
    }else{
        cell.textLabel.text = dataArray[indexPath.row][@"name"];
    }

    if ([cell.textLabel.text isEqualToString:titleStr]){
        cell.textLabel.textColor = UIColorFromRGB(0xffb619);
    }else{
        cell.textLabel.textColor = [UIColor whiteColor];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if ([_type isEqualToString:@"taocan"]) {
        [_delegate chooseType:_type data:dataArray[indexPath.row]];
    }else if ([_type isEqualToString:@"dizhi"]) {
        [_delegate chooseType:_type data:dataArray[indexPath.row]];
    }else{
        [_delegate chooseType:_type data:dataArray[indexPath.row]];

    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
